package cn.yr.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.yr.blog.mapper.CommentMapper;
import cn.yr.blog.service.CommentService;
import cn.yr.common.core.domain.entity.YrComment;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @title CommentServiceImpl
* @description 评论
* @author Lang 1102076808@qq.com
* @date 2021/3/20 20:16
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, YrComment> implements CommentService {

	/**
	 * @description
	 * @author Lang
	 * @date 2020/7/30 21:45
	 */
	@Override
	public List<Tree<Integer>> buildCommentTree(List<YrComment> list) {

		TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
		// 自定义属性名 都要默认值的
		treeNodeConfig.setIdKey("id");
		treeNodeConfig.setParentIdKey("pid");
		// 最大递归深度
		return TreeUtil.build(list, 0, treeNodeConfig,(treeNode, tree) -> {
			tree.setId(treeNode.getId());
			tree.setParentId(treeNode.getPid());
			tree.putAll(BeanUtil.beanToMap(treeNode));
		});

	}
}